package com.fishery.base.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
@ConfigurationProperties(prefix = "fishery.thread")
public class MyThreadPoolConfig  {

	private int corePoolSize; // 核心线程池大小
	private int maxinumPoolSize; // 最大核心线程池大小
	private int keepAliveTime; // 超时了没有人调用就会释放



	@Bean
	public ExecutorService executorService() {
		ExecutorService threadPool = new ThreadPoolExecutor(
				corePoolSize,
				maxinumPoolSize,
				keepAliveTime,
				TimeUnit.SECONDS,
				new LinkedBlockingDeque<>(maxinumPoolSize - corePoolSize),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.CallerRunsPolicy()
		);
		return threadPool;
	}

	public int getCorePoolSize() {
		return corePoolSize;
	}

	public void setCorePoolSize(int corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	public int getMaxinumPoolSize() {
		return maxinumPoolSize;
	}

	public void setMaxinumPoolSize(int maxinumPoolSize) {
		this.maxinumPoolSize = maxinumPoolSize;
	}

	public int getKeepAliveTime() {
		return keepAliveTime;
	}

	public void setKeepAliveTime(int keepAliveTime) {
		this.keepAliveTime = keepAliveTime;
	}
}
